package cn.iocoder.yudao.module.famous.doctor.service.treatmentuser;

import cn.hutool.core.util.IdcardUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jodd.util.StringUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.treatmentuser.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.treatmentuser.TreatmentUserDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.famous.doctor.convert.treatmentuser.TreatmentUserConvert;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.treatmentuser.TreatmentUserMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.famous.doctor.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 就诊人 Service 实现类
 *
 * @author 档案
 */
@Service
@Validated
public class TreatmentUserServiceImpl implements TreatmentUserService {

    @Resource
    private TreatmentUserMapper treatmentUserMapper;


    @Override
    public Long createTreatmentUser(TreatmentUserCreateReqVO createReqVO) {
        // 插入
        if (StringUtil.isNotBlank(createReqVO.getIdentityCard())) {
            createReqVO.setIdentityCard(createReqVO.getIdentityCard().toUpperCase());
        }
        verifyIdentityCard(createReqVO.getIdentityCard());
        QueryWrapper<TreatmentUserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TreatmentUserDO::getCreator, getLoginUserId());
        queryWrapper.lambda().eq(TreatmentUserDO::getIdentityCard, createReqVO.getIdentityCard());
        if (treatmentUserMapper.selectCount(queryWrapper) > 0) {
            throw exception(TREATMENT_USER_NOT_EXISTS_MAX);
        }
        TreatmentUserDO treatmentUser = TreatmentUserConvert.INSTANCE.convert(createReqVO);
        treatmentUserMapper.insert(treatmentUser);
        // 返回
        return treatmentUser.getId();
    }

    @Override
    public void updateTreatmentUser(TreatmentUserUpdateReqVO updateReqVO) {
        // 校验存在
        validateTreatmentUserExists(updateReqVO.getId());
        // 更新
        TreatmentUserDO updateObj = TreatmentUserConvert.INSTANCE.convert(updateReqVO);
        treatmentUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteTreatmentUser(Long id) {
        // 校验存在
        validateTreatmentUserExists(id);
        // 删除
        treatmentUserMapper.deleteById(id);
    }

    private void validateTreatmentUserExists(Long id) {
        if (treatmentUserMapper.selectById(id) == null) {
            throw exception(TREATMENT_USER_NOT_EXISTS);
        }
    }

    @Override
    public TreatmentUserDO getTreatmentUser(Long id) {
        return treatmentUserMapper.selectById(id);
    }

    @Override
    public List<TreatmentUserDO> getTreatmentUserList() {
        return treatmentUserMapper.selectList(new LambdaQueryWrapper<TreatmentUserDO>()
                .eq(TreatmentUserDO::getCreator, getLoginUserId()));
    }

    @Override
    public PageResult<TreatmentUserDO> getTreatmentUserPage(TreatmentUserPageReqVO pageReqVO) {
        return treatmentUserMapper.selectPage(pageReqVO);
    }

    @Override
    public List<TreatmentUserDO> getTreatmentUserList(TreatmentUserExportReqVO exportReqVO) {
        return treatmentUserMapper.selectList(exportReqVO);
    }

    @Override
    public IdcardUtil.Idcard verifyIdentityCard(String  card) {
        String idNum=card.toUpperCase();
        boolean validCard = IdcardUtil.isValidCard(idNum);
        if(!validCard) {
            throw exception(TREATMENT_USER_NOT_EXISTS_IDENTITYCARD);
        }
        return IdcardUtil.getIdcardInfo(idNum);
    }

}
